package com.yin.myproject.thread;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author yinxunzhi
 * Created on 2023/8/27
 */
public class ReentrantLockDemo {

    private static  int sum = 0;
    private static final Lock REENTRANT_LOCK = new ReentrantLock();

    public static void main(String[] args) throws InterruptedException {

        for (int i = 0; i < 3; i++) {
            Thread thread = new Thread(()->{
                //加锁
                REENTRANT_LOCK.lock();
                try {
                    for (int j = 0; j < 10000; j++) {
                        sum++;
                    }
                } finally {
                    // 解锁
                    REENTRANT_LOCK.unlock();
                }
            });
            thread.start();
            Thread.sleep(2000);
        }
        System.out.println(sum);
    }
}
